package com.websense.training.books

import java.io.{ File, FileOutputStream }
import org.apache.log4j.Logger

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

class BuildStamper(var pdfInFile:String,  var pdfOutFile:String) {

  val logger = org.apache.log4j.Logger
    .getLogger(classOf[BuildStamper])

  //	private String pdfInDirectory;
  //	private String pdfTemplate;
  //	private String pdfDirectory;
  //
  //	private List<MergeItem> items;
  //
  //	private List<MergeItem> staticItems;
  //	private HashMap<String, String> staticData;
  //
  //	public static String NOCREDENTIALS = "NoCredentials-";
  //
  //
  //	public ExamStamper(String pdfInDirectory, String pdfTemplate,
  //			String pdfDirectory, List<MergeItem> items,
  //			List<MergeItem> staticItems, HashMap<String, String> staticData) {
  //		this.pdfInDirectory = pdfInDirectory;
  //		this.pdfTemplate = pdfTemplate;
  //		this.pdfDirectory = pdfDirectory;
  //		this.items = items;
  //
  //		this.staticItems = staticItems;
  //		this.staticData = staticData;
  //
  //		try {
  //			File f = new File(pdfDirectory);
  //			if (!f.exists()) {
  //				boolean result = f.mkdir();
  //				logger.info("Creating output directory '" + pdfDirectory
  //						+ "' result is '" + result + "'");
  //			}
  //		} catch (Exception e) {
  //			logger.error("CRITICAL: Could not create output directory '"
  //					+ pdfDirectory + "'", e);
  //		}
  //	}
  //
  //	private String getVal(Map<String, String> dataRow, String column) {
  //		if (column.startsWith("%")) {
  //			String chop = column.substring(1, column.length() - 1);
  //			return dataRow.get(chop);
  //		} else {
  //			return column;
  //		}
  //	}
  //
  //	public void process(String pdfFilename, Map<String, String> dataRow)
  //			throws IOException, DocumentException {
  //
  //		PdfReader reader = new PdfReader(pdfDirectory + "\\" + NOCREDENTIALS
  //				+ pdfTemplate);
  //		File directory = new File(pdfDirectory);
  //		File outFile = new File(directory, pdfFilename);
  //		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
  //				outFile));
  //
  //		PdfContentByte canvas = null;
  //
  //		for (MergeItem item : items) {
  //			canvas = stamper.getOverContent(item.getPage());
  //
  //			float X0 = item.getX0();
  //			float Y0 = item.getY0();
  //
  //			ColumnText.showTextAligned(
  //					canvas,
  //					Element.ALIGN_LEFT,
  //					new Phrase(getVal(dataRow, item.getName()), FontFactory
  //							.getFont(FontFactory.TIMES, 12, Font.ITALIC,
  //									BaseColor.BLUE)), X0, Y0, 0);
  //
  //		}
  //		stamper.close();
  //	}

//  val pdfInDirectory = "resources"
//  val pdfDirectory = "resources"
//  val pdfTemplate = "empty-cover.pdf"
//  val coverPath = "my-cover.pdf"

  def processStatic(staticData: Map[String, String], staticParams: Map[String, List[Int]]): Unit = {
    val reader = new PdfReader(pdfInFile)
//    val directory = new File(pdfDirectory)
//    val outFile = new File(directory, coverPath);
    val outFile = new File(pdfOutFile)
    val stamper = new PdfStamper(reader, new FileOutputStream(
      outFile));

    var canvas: PdfContentByte = null;
    for (item <- staticParams.keys) {
      logger.info("Adding static data for item " + item);
      canvas = stamper.getOverContent(staticParams.get(item).get(0));
      val X0 = staticParams.get(item).get(1)
      val Y0 = staticParams.get(item).get(2)
      val fontSize = staticParams.get(item).get(3)
      logger.info("X0 = " + X0 + ", Y0 = " + Y0);
      logger.info("Static data for '" + item + "' is '"
        + staticData.get(item) + "'");

      // ColumnText.showTextAligned(
      // canvas,
      // Element.ALIGN_LEFT,
      // new Phrase(staticData.get(item.getName()), FontFactory
      // .getFont(FontFactory.TIMES, 12, Font.ITALIC,
      // BaseColor.BLUE)), X0, Y0, 0);
      ColumnText.showTextAligned(
        canvas,
        Element.ALIGN_LEFT,
        new Phrase(staticData.get(item).get, FontFactory
          .getFont(FontFactory.HELVETICA, fontSize,
            Font.NORMAL, BaseColor.WHITE)), X0, Y0, 0);

    }
    stamper.close();

  }
}
